package org.balthie.demo.jvm.runtime.falsesharing;

//使用  @jdk.internal.vm.annotation.Contended("group1") 注解 ，程序制定缓存行
public class FalseSharingExample03
{
    public static void main(String[] args)
    {
        Counter1 counter1 = new Counter1();
        Counter1 counter2 = counter1;
        
        long iterations = 100_000_000;
        
        Thread thread1 = new Thread(() -> {
            long startTime = System.currentTimeMillis();
            for(long i = 0; i < iterations; i++)
            {
                // thread1 操作 count1
                counter1.count1++;
            }
            long endTime = System.currentTimeMillis();
            System.out.println("total time: " + (endTime - startTime));
        });
        
        Thread thread2 = new Thread(() -> {
            long startTime = System.currentTimeMillis();
            for(long i = 0; i < iterations; i++)
            {
                // thread2 操作 count2
                counter2.count2++;
            }
            long endTime = System.currentTimeMillis();
            System.out.println("total time: " + (endTime - startTime));
        });
        
       
        thread1.start();
        thread2.start();
    }
}